﻿Imports System
Imports System.CodeDom
Imports System.Text

Namespace System.Workflow.ComponentModel.Serialization
    Friend NotInheritable Class CodeTypeReferenceSerializer
        Inherits WorkflowMarkupSerializer
        ' Methods
        Protected Friend Overrides Function CanSerializeToString(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal value As Object) As Boolean
            Return TypeOf value Is CodeTypeReference
        End Function

        Private Shared Function ConvertTypeReferenceToString(ByVal reference As CodeTypeReference) As String
            Dim builder As StringBuilder
            If (Not reference.ArrayElementType Is Nothing) Then
                builder = New StringBuilder(CodeTypeReferenceSerializer.ConvertTypeReferenceToString(reference.ArrayElementType))
                If (reference.ArrayRank > 0) Then
                    builder.Append("[")
                    builder.Append(","c, (reference.ArrayRank - 1))
                    builder.Append("]")
                End If
            Else
                builder = New StringBuilder(reference.BaseType)
                If ((Not reference.TypeArguments Is Nothing) AndAlso (reference.TypeArguments.Count > 0)) Then
                    Dim str As String = "["
                    Dim reference2 As CodeTypeReference
                    For Each reference2 In reference.TypeArguments
                        builder.Append(str)
                        builder.Append(CodeTypeReferenceSerializer.ConvertTypeReferenceToString(reference2))
                        str = ", "
                    Next
                    builder.Append("]")
                End If
            End If
            Return builder.ToString
        End Function

        Protected Friend Overrides Function DeserializeFromString(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal propertyType As Type, ByVal value As String) As Object
            Dim reference As CodeTypeReference
            If Not propertyType.IsAssignableFrom(GetType(CodeTypeReference)) Then
                Return Nothing
            End If
            If (String.IsNullOrEmpty(value) OrElse MyBase.IsValidCompactAttributeFormat(value)) Then
                Return Nothing
            End If
            Try 
                Dim type As Type = serializationManager.GetType(value)
                If (Not type Is Nothing) Then
                    reference = New CodeTypeReference(type)
                    reference.UserData.Item("QualifiedName") = type.AssemblyQualifiedName
                    Return reference
                End If
            Catch exception1 As Exception
            End Try
            reference = New CodeTypeReference(value)
            reference.UserData.Item("QualifiedName") = value
            Return reference
        End Function

        Protected Friend Overrides Function SerializeToString(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal value As Object) As String
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (value Is Nothing) Then
                Throw New ArgumentNullException("value")
            End If
            Dim reference As CodeTypeReference = [TryCast](value,CodeTypeReference)
            If (reference Is Nothing) Then
                Return String.Empty
            End If
            Dim str As String = [TryCast](reference.UserData.Item("QualifiedName"),String)
            If (Not str Is Nothing) Then
                Return str
            End If
            Dim typeName As String = CodeTypeReferenceSerializer.ConvertTypeReferenceToString(reference)
            Dim type As Type = serializationManager.GetType(typeName)
            If (type Is Nothing) Then
                type = Type.GetType(typeName, False)
                If (type Is Nothing) Then
                    Return typeName
                End If
            End If
            Return type.AssemblyQualifiedName
        End Function


        ' Fields
        Friend Const QualifiedName As String = "QualifiedName"
    End Class
End Namespace

